package com.xiaocui.shoppingmall.config;

import com.xiaocui.shoppingmall.entity.SwaggerConfigEntity;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.Resource;

/**
 * project: shoppingmall
 * package: com.xiaocui.shoppingmall.common.config
 * describe:
 *          公共配置类：swaggerui配置类
 *
 * @author vstarcui
 * @create 2018-03-06 14:53
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Resource
    private SwaggerConfigEntity swaggerConfigEntity;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage(swaggerConfigEntity.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建 api文档的详细信息函数
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title(swaggerConfigEntity.getTitle())
                //创建人
                .contact(new Contact(swaggerConfigEntity.getName(), swaggerConfigEntity.getUrl(), swaggerConfigEntity.getEmail()))
                //版本号
                .version(swaggerConfigEntity.getVersion())
                //描述
                .description(swaggerConfigEntity.getDescription())
                .build();
    }
}
